{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "29336c08-304b-4f29-95ae-01ef30a06aec",
   "metadata": {},
   "source": [
    "### 多线程版本 v1 一个并行方向\n",
    "1. 根目录子文件夹进行并行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d4ccb344-305f-4b28-bec2-a683dfea0be0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "import threading\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "98ccb046-3521-4124-aaf1-a6bd8873ab26",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_dir_size(root,directory):\n",
    "    total_size = 0\n",
    "    base_stat={}\n",
    "    baseDir = os.path.join(root, directory)\n",
    "    \n",
    "    start_time = time.time()\n",
    "    for dirpath, _, filenames in os.walk(baseDir):\n",
    "        dir_size = 0\n",
    "        for filename in filenames:\n",
    "            filepath = os.path.join(dirpath, filename)\n",
    "            try:\n",
    "                my_size = os.path.getsize(filepath)\n",
    "                total_size += my_size\n",
    "                dir_size += my_size\n",
    "            except FileNotFoundError:\n",
    "                pass\n",
    "        \n",
    "        if dir_size > 200*1024**2:\n",
    "            base_stat[dirpath] = dir_size\n",
    "    end_time = time.time() \n",
    "    print(f\"分析文件夹 {baseDir} 用时{(end_time - start_time):.2f} 秒 ---------------------\")\n",
    "    return directory, total_size, base_stat\n",
    "\n",
    "def scan_dirs(root):\n",
    "    print(f\"开始分析 Folder: {root} --------------------------------\")\n",
    "    directories = os.listdir(root) \n",
    "    statMap = {}\n",
    "    total_size = 0\n",
    "    with ThreadPoolExecutor(max_workers=8) as executor:\n",
    "        # 并发扫描多个目录\n",
    "        for size in executor.map(lambda x :get_dir_size(root,x), directories):\n",
    "            total_size += size[1]\n",
    "            statMap.update(size[2])\n",
    "        \n",
    "    #sorted_folder = dict(sorted(subfolder_sizes.items(), key=lambda item:item[1], reverse= True))\n",
    "         \n",
    "    return total_size,statMap\n",
    "\n",
    "def main(folder):\n",
    "    # 假设你有一个根目录或者一个包含多个子目录的列表\n",
    "    start_time = time.time()\n",
    "    result = scan_dirs(folder)\n",
    "    total_size = result[0]\n",
    "    statMap =  result[1]\n",
    "    end_time = time.time()\n",
    "    print(f\"{folder} Total size: {total_size / (1024 ** 3):.2f} GB, 分析耗时: {(end_time - start_time):.2f} 秒 \")\n",
    "    print(f\"开始打印瓶颈文件夹，总计 {len(statMap)}\")\n",
    "    # 打印瓶颈\n",
    "    sorted_folder = dict(sorted(statMap.items(), key=lambda item:item[1], reverse= True))    \n",
    "    for folder, size in sorted_folder.items():\n",
    "        print(f\"Folder: {folder}, size: {size / (1024**2):.2f} MB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c89bf4b2-b495-41de-980d-5f7e2e812efe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始分析 Folder: C:\\Users\\manulism\\AppData\\Roaming --------------------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\2345SafeCenter 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\360Quarant 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\afc 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\2345DomainMon 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\aDrive 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Adobe 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Apple Computer 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\baidu 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\brolst_2021.12.6 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\changzhi_leidian.data 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\changzhi2 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\BaiduYunKernel 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\D10B9980-8A16-4595-89C0-FD6EF580FAAA 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\CleanTrash 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\DCPCollNode 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\dinglive 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\BaiduYunGuanjia 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Archi4 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\DotNetBrowser 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\downloader 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Docker 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Electron 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Everything 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Expert 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\FortiClient 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Docker Desktop 用时0.07 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\GlobalMgr.db 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Foxmail7 用时0.03 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\InsLogicCfg 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\HBuilder X 用时0.04 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\jupyter 用时0.08 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\k52zip 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\DBeaverData 用时0.24 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\kingsoft 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\lddownloader 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\ldremote 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\ldremote.exe.ZEGO.SDK 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\lds 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\LDSGameCenter 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\JetBrains 用时0.26 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\leidian 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\leidian9 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\lockhomepage 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Ludashi 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\manulism 用时0.09 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\ee 用时0.46 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\lebo 用时0.22 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Microsoft Corporation 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\MMReminder 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Mozilla 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\MYNovel 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Cursor 用时0.51 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\node-red-opcua-nodejs 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\node-opcua-default-nodejs 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\notepad 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\DingTalk 用时0.79 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\NuGet 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Nutstore 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\NVIDIA 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\mcm 用时0.80 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\obs-virtualcam.txt 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\PDManer 用时0.03 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\PLogs 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\apifox 用时1.33 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\RabbitMQ 用时0.18 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\robot 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\kdesktop 用时1.31 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\setting.json 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\sh 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\SAS 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Shield_2345Explorer 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Sun 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Subversion 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\TabXExplorer 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\tabx_pp 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\TeamViewer 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Thycotic 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\TortoiseGit 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\TortoiseSVN 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\TPDownloadProxy 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Typora 用时0.22 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Updater 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\utForpc 用时0.02 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Visual Studio Setup 用时0.01 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\vlc 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\VMware 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\VoipNNModel 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\wemeetapp 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\WinRAR 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\WXDrive 用时0.04 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\xra 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\yiwanzhushou 用时0.00 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\点读笔助手 用时0.05 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\qq_guild 用时0.73 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Code 用时3.44 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Microsoft 用时3.21 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\Tencent 用时5.74 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\npm-cache 用时10.66 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\npm 用时17.59 秒 ---------------------\n",
      "分析文件夹 C:\\Users\\manulism\\AppData\\Roaming\\nvm 用时18.07 秒 ---------------------\n",
      "C:\\Users\\manulism\\AppData\\Roaming Total size: 7.53 GB, 分析耗时: 18.92 秒 \n",
      "开始打印瓶颈文件夹，总计 5\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Tencent\\WeChat\\XPlugin\\Plugins\\RadiumWMPF\\13839\\extracted\\runtime, size: 315.14 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Tencent\\WeChat\\XPlugin\\Plugins\\RadiumWMPF\\13639\\extracted\\runtime, size: 315.13 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Tencent\\WeChat\\XPlugin\\Plugins\\RadiumWMPF\\13487\\extracted\\runtime, size: 311.55 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Tencent\\WXWork\\wmpf_Applet\\2.1.3.11254\\wmpf_Applet\\runtime, size: 258.35 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\kdesktop\\Cache\\Cache_Data, size: 202.57 MB\n"
     ]
    }
   ],
   "source": [
    "folder = \"C:\\\\Users\\manulism\\AppData\\Roaming\"\n",
    "main(folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c1c4469-fdf3-42bc-8a0e-da96624a273c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61a48a81-f253-4ca9-9166-adb774217ad5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
